package com.mjk.common.web.getway.conf.exception;


import cn.hutool.core.exceptions.ExceptionUtil;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.util.MDCUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

/**
 * 统一异常处理
 * @author mjk
 *
 */
@RestControllerAdvice 
public class BaseExceptionHandler {
	/**
	 * 应用到所用的@Requestmapping,在其执行之前初始化数据绑定器
	 */
	@InitBinder	
	public void initWebBinder(WebDataBinder binder){
		//binder.addCustomFormatter(formatter);
		
	}
	@ModelAttribute
	public void addAttribute(Model model){

	}
	/**
	 * 捕获bizexception
	 * @param exception
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(Exception.class)
	public BeeResponse customExceptionHandler(Exception exception){
        BeeResponse rsp = BeeError.BEE_ERROR_FOR_ADMIN.tranceError(ExceptionUtil.getMessage(exception));
		rsp.setTraceId(MDCUtils.getTraceId());
		BizLogger.error(exception);
		return rsp;
	}
}
